#!/bin/bash

#
# wanip -- by Dario Berzano <dario.berzano@gmail.com>
#
# Part of the lfscripts suite -- http://lfscripts.googecode.com/
#
# Connects to a remote server to get current IP address. If -q option is given
# it returns the IP address only without additional messages. It works with
# both wget and curl.
#

#
# Variables
#

# URL that returns a plain IP address, without HTML and stuff
#export IPURL="http://automation.whatismyip.com/n09230945.asp"
export IPURL="http://newton.ph.unito.it/~berzano/ip"

#
# Functions
#

# Uses wget or curl
function Download() {
  which wget > /dev/null 2>&1
  if [ $? == 0 ]; then
    wget -q -O - "$1"
  else
    which curl > /dev/null 2>&1
    if [ $? == 0 ]; then
      curl "$1"
    fi
  fi
}

# Main function
function Main() {
  local IP=$(Download "$IPURL")
  [ "$1" == "-q" ] && echo $IP || echo "Your WAN IP address is: $IP"
}

#
# Entry point
#

Main "$@"
